module pk {

    export class PkFriend extends egret.DisplayObjectContainer {

        private headImg: egret.Bitmap;
        private beyondTxt: egret.TextField;
        private levelTxt: egret.TextField;
        private nickTxt: egret.TextField;

        public constructor() {
            super();

            this.getFriendPkData();

            let yDiff: number = 3;

            this.beyondTxt = new egret.TextField();
            this.beyondTxt.size = 16;
            this.beyondTxt.textColor = 0x0;
            this.beyondTxt.text = "即将超越";
            this.addChild(this.beyondTxt);
            this.beyondTxt.y = yDiff;


            this.headImg = new egret.Bitmap();
            this.headImg.width = this.headImg.height = 80;
            this.addChild(this.headImg);
            this.headImg.y = this.beyondTxt.height + this.beyondTxt.y + yDiff;


            this.nickTxt = new egret.TextField();
            this.nickTxt.size = 16;
            this.nickTxt.textColor = 0x0;
            this.addChild(this.nickTxt);
            this.nickTxt.y = this.headImg.y + this.headImg.height + yDiff;
            this.nickTxt.text = "00";

            this.levelTxt = new egret.TextField();
            this.levelTxt.size = 28;
            this.levelTxt.textColor = 0xcb6405;
            this.addChild(this.levelTxt);
            this.levelTxt.y = this.nickTxt.y + this.nickTxt.height + yDiff;
        }

        static readonly LOADED: number = 2;

        private currentIdx: number = -1;

        private currentScore: number;

        public pk(score: number): void {
            if (this.isLoading < 2) {
                return;
            }
            let len: number = this.friends.length;
            let i: number = 0;
            for (; i < len; i++) {
                if (score < this.friends[i].score) {
                    if (this.currentIdx != i) {
                        this.update(this.friends[i])
                    }
                    this.visible = true;
                    this.currentIdx = i;
                    break;
                }
            }
            if (i >= len) {
                this.update(this.friends[len - 1]);
                this.beyondTxt.text = "登峰造极";
            } else {
                this.beyondTxt.text = "即将超越";
            }
        }

        private imgLoader: egret.ImageLoader;
        private currentFriend: rank.ScoreItem;

        public static readonly WIDTH: number = 200;

        private update(friend: rank.ScoreItem): void {
            this.currentFriend = friend;
            this.nickTxt.text = friend.nick;
            this.levelTxt.text = "第" + friend.score + "关";
            //
            this.nickTxt.x = (PkFriend.WIDTH - this.nickTxt.width) / 2;
            this.levelTxt.x = (PkFriend.WIDTH - this.levelTxt.width) / 2;
            this.beyondTxt.x = (PkFriend.WIDTH - this.beyondTxt.width) / 2;

            this.headImg.x = (PkFriend.WIDTH - this.headImg.width) / 2;

            this.loadImg();
        }

        public loadImg() {
            this.imgLoader = new egret.ImageLoader();
            this.imgLoader.once(egret.Event.COMPLETE, this.loadCompleteHandler, this);
            this.imgLoader.load(this.currentFriend.avatarUrl);
        }

        private loadCompleteHandler(event: egret.Event): void {
            let imageLoader = <egret.ImageLoader>event.currentTarget;
            imageLoader.removeEventListener(egret.Event.COMPLETE, this.loadCompleteHandler, this);
            let texture = new egret.Texture();
            texture._setBitmapData(imageLoader.data);
            this.headImg.texture = texture;
        }

        public friends: Array<rank.ScoreItem> = [];

        private isLoading: number = 0;

        private getFriendPkData() {
            if (this.isLoading > 0) {
                return;
            }
            this.isLoading = 1;
            let that = this;
            wx.getFriendCloudStorage({
                keyList: ["score"],
                success: res => {
                    that.friends = Main.format(res.data, false);
                    egret.log("朋友数据:", that.friends);
                },
                fail: err => {
                    console.log(err);
                },
                complete: () => {
                    that.isLoading = PkFriend.LOADED;
                    that.pk(that.currentScore);
                }
            });
        }

    }

}